package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.UUID;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

/**
 * @author sunyamei
 * @version V1.0
 * @Project javase2203
 * @Package com.qf.service
 * @Description:
 * @Date 2022/7/27 11:19
 */
@Service
public class StorageServiceImpl implements  StorageService {

    //服务器地址
    @Value("${aliyun.oss.endpoint}")
    private  String endpoint;
    
    //子账号
    @Value("${aliyun.oss.accessKeyId}")
    private  String accessKeyId;
    
    //子账号密码
    @Value("${aliyun.oss.accessKeySecret}")
    private  String accessKeySecret;
    
    //桶的名字
    @Value("${aliyun.oss.bucketName}")
    private  String bucketName;
    
    
    
    
   @Autowired
   private DtsStorageMapper storageMapper;

    @Override
    public DtsStorage add(String filename, String ext, long size, byte[] content) throws Exception {
        
        //1:生产一个随机字符串作为文件名
        String key = UUID.randomUUID().toString();
        //2:随机的字符串+文件扩展名组成文件名称
        String newfilename = key+ext;
        //3:文件上传到阿里云
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, newfilename, content);
        //4:封装文件对象
        DtsStorage storage = new DtsStorage();
        storage.setUpdateTime(new Date());
        storage.setAddTime(new Date());
        storage.setDeleted(false);
        storage.setUrl(url);
        storage.setType(ext);
        storage.setSize((int)size);
        storage.setName(filename);
        storage.setKey(key);
        //5:将文件对象保存到表中
        int count = storageMapper.insert(storage);

        return  storage;
    }



    /**
     * 对象列表里面的参数
     * 分页查询
     * list?page=1&limit=20&sort=add_time&order=desc
     * @return
     * @param page  当前页
     * @param limit 每页查询条数
     * @param sort  排序字段名
     * @param order 排序方式
     * @param key   根据key查询
     * @param name  根据名字查询
     */
    @Override
    public IPage<DtsStorage> queryByPage(Integer page, Integer limit, String sort, String order, String key, String name) {

        //分页对象,第一参数:当前页,第二参数,每页显示条数
        IPage<DtsStorage> ipage = new Page<>(page, limit);

        //创建查询条件
        QueryWrapper<DtsStorage> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)){
            queryWrapper.eq("key", key);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.eq("name", name);
        }

        //排序
        if ("asc".equalsIgnoreCase(order)){
            queryWrapper.orderByAsc(sort);
        }else {
            queryWrapper.orderByDesc(sort);
        }

         ipage = storageMapper.selectPage(ipage, queryWrapper);

        return ipage;
    }



}
